/***************************************************************************
 * Copyright (C) 2013 Dj_Skual
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you
 * must not claim that you wrote the original software. If you use
 * this software in a product, an acknowledgment in the product
 * documentation would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 *
 * DevSaveButtons.cpp
 *
 * for SaveGame Manager GX
 ***************************************************************************/
#include "DevSaveButtons.hpp"
#include "DevSaveTask.hpp"
#include "../ManageTools.h"
#include "../../Controls/Application.h"
#include "../../Menu/Explorer.h"

enum
{
	INSTALL_DEV_SAVE = 1,
	DELETE_DEV_SAVE,
	MOVE_DEV_SAVE,
	COPY_DEV_SAVE
};

DevSaveButtons::DevSaveButtons()
	: CommonButtons()
{
	installButtons = new ManageButtons(tr( "Install to:" ),
									   0,
									   tr( "Wii" ),
									   tr( "Emu" ),
									   tr( "Cancel" ));
	installButtons->ButtonClicked.connect(this, &DevSaveButtons::OnInstallClick);
	
	SetENandValues();
	
	emuButtons = new ManageButtons(tr( "Install to:" ),
								   0,
								   emu1 ? tr( "Emu1" ) : NULL,
								   emu2 ? tr( "Emu2" ) : NULL,
								   emu3 ? tr( "Emu3" ) : NULL,
								   tr( "Cancel" ));
	emuButtons->ButtonClicked.connect(this, &DevSaveButtons::OnEmuClick);
	
	pathButtons = new ManageButtons(tr( "Move to:" ), 0, tr( "Default" ), tr( "Custom" ), tr( "Cancel" ));
	pathButtons->ButtonClicked.connect(this, &DevSaveButtons::OnPathClick);
}

DevSaveButtons::~DevSaveButtons()
{
	RemoveAll();

	delete installButtons;
	delete emuButtons;
	delete pathButtons;
}

void DevSaveButtons::OnActionClick(ManageButtons *buttons UNUSED, int choice)
{
	action = choice;
	BlockButtons(true);
	HideButtons(actionButtons);
	
	if(action == INSTALL_DEV_SAVE)
	{
		if(!Settings.SneekRunning && Settings.EmuBrowsers && eNandCount)
		{
			if(installed)
				ShowButtons(installButtons);
			else
			{
				installType = INSTALL_DEV_ENAND;
				
				if(eNandCount > 1)
				{
					ShowButtons(emuButtons);
				}
				else
				{
					confirmButtons->SetTitle(tr( "Install to:" ));
					
					if(emu1)
						Settings.TmpPath = Settings.EmuNandPath_1;
					else if(emu2)
						Settings.TmpPath = Settings.EmuNandPath_2;
					else if(emu3)
						Settings.TmpPath = Settings.EmuNandPath_3;
					
					Confirm();
				}
			}
		}
		else
		{
			installType = INSTALL_DEV_NAND;
			
			confirmButtons->SetTitle(tr( "Install from:" ));
			Settings.TmpPath = CurrentBrowser()->GetCurrentPath();
			
			Confirm();
		}
	}
	else if(action == DELETE_DEV_SAVE)
	{
		confirmButtons->SetTitle(tr( "Delete from:" ));
		Settings.TmpPath = Settings.LastUsedPath;
		
		Confirm();
	}
	else if (action == MOVE_DEV_SAVE || action == COPY_DEV_SAVE)
	{
		if(action == MOVE_DEV_SAVE) pathButtons->SetTitle(tr( "Move to:" ));
		else if(action == COPY_DEV_SAVE) pathButtons->SetTitle(tr( "Copy to:" ));
		
		ShowButtons(pathButtons);
	}
}

void DevSaveButtons::OnInstallClick(ManageButtons *buttons UNUSED, int choice)
{
	HideButtons(installButtons);
	
	if(choice == 1)
	{
		installType = INSTALL_DEV_NAND;
		
		confirmButtons->SetTitle(tr( "Install from:" ));
		Settings.TmpPath = CurrentBrowser()->GetCurrentPath();
		
		Confirm();
	}
	else if(choice == 2)
	{
		installType = INSTALL_DEV_ENAND;
		
		if(eNandCount > 1)
		{
			ShowButtons(emuButtons);
		}
		else
		{
			confirmButtons->SetTitle(tr( "Install to:" ));
			
			if(emu1)
				Settings.TmpPath = Settings.EmuNandPath_1;
			else if(emu2)
				Settings.TmpPath = Settings.EmuNandPath_2;
			else if(emu3)
				Settings.TmpPath = Settings.EmuNandPath_3;
			
			Confirm();
		}
	}
	else
		Cancel();
}

void DevSaveButtons::OnEmuClick(ManageButtons *buttons UNUSED, int choice)
{
	HideButtons(emuButtons);
	
	if(choice < 4)
	{
		confirmButtons->SetTitle(tr( "Install to:" ));
		Settings.TmpPath = ManageTools::GetENandPath(choice);
		
		Confirm();
	}
	else
	{
		Cancel();
	}
}

void DevSaveButtons::OnPathClick(ManageButtons *buttons UNUSED, int choice)
{
	HideButtons(pathButtons);
	
	if(action == MOVE_DEV_SAVE) confirmButtons->SetTitle(tr( "Move to:" ));
	else if(action == COPY_DEV_SAVE) confirmButtons->SetTitle(tr( "Copy to:" ));
	
	if(choice == 1) //! Default
	{
		if(!Settings.LastUsedPath.compare(Settings.SaveGamePath))
		{
			errorButtons->SetMessage(tr( "Source and destination folders must be different" ));
			
			ShowButtons(errorButtons);
		}
		else
		{
			Settings.TmpPath = Settings.SaveGamePath;
			
			Confirm();
		}
	}
	else if(choice == 2) //! Custom
	{
		PathBrowser *pathBrowser = new PathBrowser(Settings.LastUsedPath);
		pathBrowser->ButtonClick.connect(this, &DevSaveButtons::OnPathBrowserButtonClick);
	}
	else if(choice == 3) //! Cancel
	{
		Cancel();
	}
}

void DevSaveButtons::OnPathBrowserButtonClick(PathBrowser *menu, u8 filter UNUSED, bool doneClicked, const std::string &Path)
{
	menu->SetEffect(EFFECT_FADE, -20);
	while(menu->IsAnimated())
		Application::Instance()->updateEvents();
	menu->Close();
	
	if(doneClicked)
	{
		if(!Settings.LastUsedPath.compare(Path))
		{
			errorButtons->SetMessage(tr( "Source and destination folders must be different" ));
			
			ShowButtons(errorButtons);
		}
		else
		{
			Settings.TmpPath = Path;
			
			Confirm();
		}
	}
	else
	{
		Cancel();
	}
}

void DevSaveButtons::OnConfirmClick(ManageButtons *buttons UNUSED, int choice)
{
	HideButtons(confirmButtons);
	
	if(choice == 1)
	{
		DevSaveTask * task = NULL;
		
		if(action == INSTALL_DEV_SAVE)
		{
			if(installType == INSTALL_DEV_NAND)
				task = new DevSaveTask(DevSaveTask::INSTALL_SAVE_TO_NAND);
			else
				task = new DevSaveTask(DevSaveTask::INSTALL_SAVE_TO_ENAND);
		}
		else if(action == DELETE_DEV_SAVE)
			task = new DevSaveTask(DevSaveTask::DELETE_SAVE_FROM_DEV);
		else if(action == MOVE_DEV_SAVE)
			task = new DevSaveTask(DevSaveTask::MOVE_SAVE_TO_DEV);
		else if(action == COPY_DEV_SAVE)
			task = new DevSaveTask(DevSaveTask::COPY_SAVE_TO_DEV);
		
		task->End.connect(this, &DevSaveButtons::OnTaskEnd);
	}
	else //! Cancel
	{
		Cancel();
	}
}

void DevSaveButtons::OnTaskEnd(GuiElement * task, int result)
{
	delete task;
	
	if(result > 0)
	{
		if(action == INSTALL_DEV_SAVE) succesButtons->SetTitle(tr( "Succesfully installed" ));
		else if (action == COPY_DEV_SAVE) succesButtons->SetTitle(tr( "Succesfully copied" ));
		else if (action == MOVE_DEV_SAVE)
		{
			deleted = true;
			succesButtons->SetTitle(tr( "Succesfully moved" ));
		}
		else if (action == DELETE_DEV_SAVE)
		{
			deleted = true;
			succesButtons->SetTitle(tr( "Succesfully deleted" ));
		}
		
		ShowButtons(succesButtons);
	}
	else if(result == 0)
	{
		if(action == INSTALL_DEV_SAVE) cancelButtons->SetTitle(tr( "Install cancelled" ));
		else if (action == DELETE_DEV_SAVE) cancelButtons->SetTitle(tr( "Delete cancelled" ));
		else if (action == MOVE_DEV_SAVE) cancelButtons->SetTitle(tr( "Move cancelled" ));
		else cancelButtons->SetTitle(tr( "Copy cancelled" ));
		
		ShowButtons(cancelButtons);
	}
	else if(result < 0)
	{
		if (action == DELETE_DEV_SAVE) errorButtons->SetMessage(tr( "Unable to delete datas" ));
		else if (action == MOVE_DEV_SAVE) errorButtons->SetMessage(tr( "Unable to move datas" ));
		else errorButtons->SetMessage(tr( "Unable to copy datas" ));
		
		ShowButtons(errorButtons);
	}
}

void DevSaveButtons::SetActionButtons()
{
	deleted = false;
	
	Remove(actionButtons);
	if(actionButtons)
		delete actionButtons;
	
	installed = !CurrentBrowser()->IsNotInstalled(CurrentBrowser()->GetSelIndex());
	bool display_install = false;
	if(installed || (!Settings.SneekRunning && Settings.EmuBrowsers && eNandCount))
		display_install = true;
	
	actionButtons = new ManageButtons(0,
									  0,
									  display_install ? tr( "Install" ) : NULL,
									  tr( "Delete" ),
									  tr( "Move" ),
									  tr( "Copy" ));
	actionButtons->SetPosition(0, installed ? 0 : -25);
	actionButtons->ButtonClicked.connect(this, &DevSaveButtons::OnActionClick);
	
	Append(actionButtons);
}
